package com.bosssoft.trainee.nontax.controller;

import com.bosssoft.trainee.nontax.annotation.UserLoginToken;
import com.bosssoft.trainee.nontax.common.result.CodeMsg;
import com.bosssoft.trainee.nontax.common.result.Result;
import com.bosssoft.trainee.nontax.entity.dto.TreeNode;
import com.bosssoft.trainee.nontax.service.AuthorizeService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedList;
import java.util.List;

/**
 * @Description: 授权控制类
 * @Author: Luo WenLong
 * @Date: 2022/6/24 22:29
 */
@Slf4j
@Controller
public class AuthorizeController {

    @Autowired
    AuthorizeService authorizeService;

    /**
     * @Description: 获取所有的页面以及页面操作
     * @param: null
     * @return: com.bosssoft.trainee.nontax.common.result.Result<java.util.List<com.bosssoft.trainee.nontax.entity.dto.TreeNode>>
     * @Author: Luo WenLong
     * @Date: 2022/6/24 22:31
     */
    @UserLoginToken
    @RequestMapping("/authorize/getAllResourceAndAllOptions")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "getAllResourceAndAllOptionsHystrix")
    Result<List<TreeNode>> getAllResourceAndAllOptions() {
        return Result.success(authorizeService.getAllResourceAndAllOptions());
    }

    /**
     * @Description: 服务熔断方法
     * @return: com.bosssoft.trainee.nontax.common.result.Result<java.util.List<com.bosssoft.trainee.nontax.entity.dto.TreeNode>>
     * @Author: Luo WenLong
     * @Date: 2022/7/11 22:01
     */
    Result<List<TreeNode>> getAllResourceAndAllOptionsHystrix() {
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        treeNodes.add(TreeNode.builder().id(-1).label("no use data, please refresh later").build());
        return Result.success(treeNodes);
    }

    /**
     * @Description: 获取角色的授权操作
     * @param roleId role id
     * @return: com.bosssoft.trainee.nontax.common.result.Result<java.util.List<java.lang.Integer>>
     * @Author: Luo WenLong
     * @Date: 2022/6/24 22:32
     */
    @UserLoginToken
    @RequestMapping("/authorize/getPermissionIdsByRoleId")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "getPermissionIdsHystrix")
    Result<List<Integer>> getPermissionIds(@RequestParam Integer roleId) {
        List<Integer> rolePermissionIds = authorizeService.getRolePermissionIds(roleId);
        return rolePermissionIds != null ?
                Result.success(rolePermissionIds): Result.error(CodeMsg.SERVER_EXCEPTION);
    }

    Result<List<Integer>> getPermissionIdsHystrix(Integer roleId) {
        List<Integer> rolePermissionIds = new LinkedList<>();
        rolePermissionIds.add(-1);
        return Result.success(rolePermissionIds);
    }


    /**
     * @Description:
     * @param roleId 角色id
     * @param permissionIds permission id
     * @return Result
     */
    @UserLoginToken
    @RequestMapping("/authorize/changeRolesPermission")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "changeRolesPermissionHystrix")
    Result<Object> changeRolesPermission(
            @RequestParam(name = "roleId") Integer roleId,
            @RequestParam(name = "permissionIds") List<Integer> permissionIds) {
        log.info("request /authorize/changeRolesPermission with params roleId:{}, permissionIds: {}",roleId,permissionIds);
        return authorizeService.changeRolePermission(roleId,permissionIds) > 0?
                Result.success() : Result.error(CodeMsg.SERVER_EXCEPTION);
    }

    Result<Object> changeRolesPermissionHystrix(Integer roleId, List<Integer> permissionIds) {
        log.info("request /authorize/changeRolesPermission with params roleId:{}, permissionIds: {}",roleId,permissionIds);
        return Result.error(CodeMsg.HYSTRIX_METHOD);
    }
    /**
     * @Author WenLong Luo
     * @Description 角色授权给用户 或撤销用户授权角色方法
     * @Date 22:11 2022/6/21
     * @Param roleId
     * @return com.bosssoft.trainee.nontax.common.result.Result
     **/
    @UserLoginToken
    @RequestMapping("/authorize/authorizeRoleToUser")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "authorizeRoleToUserHystrix")
    Result<Object> authorizeRoleToUser(
            @RequestParam(name = "roleId") Integer roleId,
            @RequestParam(name = "userId") Integer userId,
            @RequestParam(name = "optCode") int optCode) {
        if(roleId == null ||roleId < 0 || userId == null || userId < 0)
            return Result.error(CodeMsg.PARAMETER_ISNULL);
        log.info("authorize role to user with opt: {}, role id: {}, user id: {}",optCode, roleId, userId);
        return authorizeService.changeUserRole(roleId,userId,optCode > 0)?
                Result.success() : Result.error(CodeMsg.SERVER_EXCEPTION);
    }

    Result<Object> authorizeRoleToUserHystrix(Integer roleId, Integer userId, int optCode) {
        log.info("authorize role to user with opt: {}, role id: {}, user id: {}",optCode, roleId, userId);
        return Result.error(CodeMsg.HYSTRIX_METHOD);
    }
}
